package com.lzm.ydpt.module.mall.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.base.BaseFragment;
import com.byt.framlib.base.TabFragmentAdapter;
import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.commonwidget.NoScrollViewPager;
import com.byt.framlib.commonwidget.StatusBarUtil;
import com.lzm.ydpt.R;
import com.lzm.ydpt.module.mall.fragment.MallCartFragment;
import com.lzm.ydpt.module.mall.fragment.MallHomeFragment;
import com.lzm.ydpt.module.mall.fragment.MallMineFragment;
import com.lzm.ydpt.module.mall.fragment.MallNearbyShopFragment;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商城首页
 */
public class MallMainActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.nsvp_mall_main)
    NoScrollViewPager nsvp_mall_main;

    private RelativeLayout[] arrayView;
    private ImageView[] arrayImg;
    private TextView[] arrayTV;
    private ArrayList<BaseFragment> fragmentList = new ArrayList<>();
    private TabFragmentAdapter mAdapters;
    private MallHomeFragment mallHomeFragment = null;
    private MallNearbyShopFragment mallShopFragment = null;
    private MallCartFragment mallCartFragment = null;
    private MallMineFragment mallMineFragment = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mall_main;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        isShow = false;
        super.onCreate(savedInstanceState);
    }

    @Override
    public BasePresenter initPreData() {
        return null;
    }

    @Override
    public void initView() {
        initWidget();
    }


    @SuppressLint("NewApi")
    private void initWidget() {
        arrayImg = new ImageView[4];
        arrayView = new RelativeLayout[4];
        arrayTV = new TextView[4];
        arrayImg[0] = findViewById(R.id.img_mall_home);
        arrayImg[1] = findViewById(R.id.img_mall_shop);
        arrayImg[2] = findViewById(R.id.img_mall_cart);
        arrayImg[3] = findViewById(R.id.img_mall_mine);
        arrayView[0] = findViewById(R.id.rl_mall_home);
        arrayView[1] = findViewById(R.id.rl_mall_shop);
        arrayView[2] = findViewById(R.id.rl_mall_cart);
        arrayView[3] = findViewById(R.id.rl_mall_mine);
        arrayTV[0] = findViewById(R.id.tv_mall_home);
        arrayTV[1] = findViewById(R.id.tv_mall_shop);
        arrayTV[2] = findViewById(R.id.tv_mall_cart);
        arrayTV[3] = findViewById(R.id.tv_mall_mine);
        mallHomeFragment = MallHomeFragment.newsInstance();
        fragmentList.add(mallHomeFragment);
        mallShopFragment = MallNearbyShopFragment.newsInstance();
        fragmentList.add(mallShopFragment);
        mallCartFragment = MallCartFragment.newsInstance();
        fragmentList.add(mallCartFragment);
        mallMineFragment = MallMineFragment.newsInstance();
        fragmentList.add(mallMineFragment);
        mAdapters = new TabFragmentAdapter(getSupportFragmentManager(), fragmentList);
        nsvp_mall_main.setAdapter(mAdapters);
        nsvp_mall_main.setOffscreenPageLimit(fragmentList.size());
        for (int i = 0, length = arrayView.length; i < length; i++) {
            arrayView[i].setOnClickListener(this);
        }
        setCurrentSelect(0);
    }

    private void setCurrentSelect(int current) {
        nsvp_mall_main.setCurrentItem(current, false);
        for (int i = 0, length = arrayImg.length; i < length; i++) {
            arrayImg[i].setSelected(current == i);
            arrayTV[i].setSelected(current == i);
        }
    }

    @OnClick({R.id.rl_mall_home, R.id.rl_mall_shop, R.id.rl_mall_cart, R.id.rl_mall_mine})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_mall_home:
                setCurrentSelect(0);
                changStatusIconCollor(false);
                break;
            case R.id.rl_mall_shop:
                setCurrentSelect(1);
                changStatusIconCollor(true);
                break;
            case R.id.rl_mall_cart:
                setCurrentSelect(2);
                changStatusIconCollor(true);
                break;
            case R.id.rl_mall_mine:
                setCurrentSelect(3);
                changStatusIconCollor(false);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.setTranslucentForImageViewInFragment(this, null);
        changStatusIconCollor(true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == MallNearbyShopFragment.address_code) {
                if (mallShopFragment != null){
                    mallShopFragment.onChangeAddress(data);
                }
            }
        }
    }
}